/*
 *jssdk jsapi所在服务器域名，无需追加'http://'或'https://'
 */
let jssdkconfigUrl = 'isdapp.shandong.gov.cn' // 爱山东测试环境

let jssdkbaseUrl = jssdkconfigUrl + '/jpaas-jags-server/interface/'
var jssdkconfig = {
	urlConfig: {
		/*
		 * 服务配置项
		 */
		urldomain: jssdkconfigUrl + '/jmopenpub/jmopen_files/js',
		baseUrl: jssdkbaseUrl,
		urldomaincreatesign: jssdkbaseUrl + 'createsign',
		urldomaingateway: jssdkbaseUrl + 'gateway',

		/*
		 * 埋点配置项
		 */
		app_id: 'jmopennzjk', //appid参数
		interface_id: 'getAppStatisticsSecret', // 上报统计数据参数
		interface_id_ua: 'checkUaAvailability' // ua判断参数
	}
}
var urldomain = jssdkconfig.urlConfig.urldomain;
// var urldomain = "172.16.11.156:8848";
var urldomaincreatesign = jssdkconfig.urlConfig.urldomaincreatesign;
var urldomaingateway = jssdkconfig.urlConfig.urldomaingateway;

var flySrc = urldomain + '/jssdk/fly.js';
var sUserAgent = navigator.userAgent.toLowerCase()
var bIsGftApp = sUserAgent.indexOf('hanweb_gft') > -1 || sUserAgent.indexOf('ganfutong_ios') > -1;

function containerType() {
	var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"
	var bIsApp = sUserAgent.indexOf('hanweb') > -1
	var bIsdingding = sUserAgent.indexOf('dingtalk') > -1
	var bIsWechat = sUserAgent.indexOf('micromessenger') > -1
	var bIsWechatMini = sUserAgent.indexOf('miniprogram') > -1
	var bIsAlipay = sUserAgent.indexOf('alipayclient') > -1
	if (bIsApp) {
		return "hanweb"
	} else if (bIsGftApp) {
		return "hanweb"
	} else if (bIsWechat) {
		if (bIsWechatMini) {
			return "wechatMini"
		}
		return "wechat"
	} else if (bIsAlipay) {
		return "Alipay"
	} else if (bIsdingding) {
		return "dingtalk"
	} else {
		return "web"
	}
}
var container = containerType()
var tmpTag = 'https:' == document.location.protocol ? true : false
// 公用js文件写入
// ua判断 及 应用埋点 js写入
if (tmpTag) {
	document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/md5.js"></script>');
	document.write('<script type="text/javascript" src="https://' + flySrc + '"></script>');
	document.write('<script type="text/javascript" src="https://' + urldomain +
		'/jssdk/AnalyticsJs/sendAnalytics.js"></script>');
} else {
	document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/md5.js"></script>');
	document.write('<script type="text/javascript" src="http://' + flySrc + '"></script>');
	document.write('<script type="text/javascript" src="http://' + urldomain +
		'/jssdk/AnalyticsJs/sendAnalytics.js"></script>');
}
if (bIsGftApp) {
	//	localStorage.setItem("gft_user", "");
	document.write(
		'<script type="text/javascript" src="https://ganfutong.jiangxi.gov.cn/jmopen/webapp/html5/gftAPI/hanwebAPI-2.0.0.min.js"></script>'
	);
} else {
	if (tmpTag == true) {
		document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/aes.js"></script>');
	} else {
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/aes.js"></script>');
	}
}
if (container == 'hanweb') {
	if (tmpTag == true) {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="https://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/sha1.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/js/indexnew.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain +
			'/jssdk/jmportal_SDK.js"></script>')
	} else {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="http://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/sha1.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/js/indexnew.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/jmportal_SDK.js"></script>')
	}

} else if (container == 'wechatMini') {

	if (tmpTag == true) {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="https://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>')
		document.write('<script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/sha1.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain +
			'/jssdk/wechat_mini/aes.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain +
			'/jssdk/wechat_mini/newsm2.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain +
			'/jssdk/wechat_mini/indexnew.js"></script>')

	} else {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="http://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>')
		document.write('<script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/sha1.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain +
			'/jssdk/wechat_mini/aes.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain +
			'/jssdk/wechat_mini/newsm2.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain +
			'/jssdk/wechat_mini/indexnew.js"></script>')
	}

} else if (container == 'wechat') {

	if (tmpTag == true) {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="https://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>')
		// document.write(
		// 	'<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=cL8AUF5vdmRtXC1cjgq2XjjS"></script>'
		// )
		document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/sha1.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain +
			'/jssdk/alipayjs/aes.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain +
			'/jssdk/alipayjs/pad-nopadding.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain +
			'/jssdk/wechatjs/indexnew.js"></script>')

	} else {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="http://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		// document.write(
		// 	'<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=cL8AUF5vdmRtXC1cjgq2XjjS"></script>'
		// )
		document.write('<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/sha1.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/alipayjs/aes.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain +
			'/jssdk/alipayjs/pad-nopadding.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain +
			'/jssdk/wechatjs/indexnew.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/alipayjs/md5.js"></script>')
	}

} else if (container == 'dingtalk') {

	if (tmpTag == true) {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="https://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/sha1.js"></script>')
		// document.write(
		// 	'<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=xOWZlWcTZPK84VcK3Ixzqq9wQ6arTUry"></script>'
		// )

	} else {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="http://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/sha1.js"></script>')
		// document.write(
		// 	'<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=xOWZlWcTZPK84VcK3Ixzqq9wQ6arTUry"></script>'
		// )
	}

} else if (container == 'Alipay') {
	document.write('<script type="text/javascript" src="https://' + urldomain +
		'/jssdk/jquery-1.8.3.min.js"></script>')
	document.write(
		'<script type="text/javascript" src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.min.js"></script>'
	)
	document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>')
	// document.write(
	// 	'<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=xOWZlWcTZPK84VcK3Ixzqq9wQ6arTUry"></script>'
	// )
	document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/alipayjs/aes.js"></script>')
	document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/sha1.js"></script>')
	document.write('<script type="text/javascript" src="https://' + urldomain +
		'/jssdk/alipayjs/pad-nopadding.js"></script>')
	document.write('<script type="text/javascript" src="https://' + urldomain +
		'/jssdk/alipayjs/indexnew.js"></script>')
} else {
	if (tmpTag == true) {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="https://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script type="text/javascript" src="https://' + urldomain + '/jssdk/sha1.js"></script>')
		document.write('<script type="text/javascript" src="https://' + urldomain +
			'/jssdk/wechatjs/brower.js"></script>');
	} else {
		if (typeof $ == 'undefined') {
			document.write('<script type="text/javascript" src="http://' + urldomain +
				'/jssdk/jquery-1.8.3.min.js"></script>')
		}
		document.write('<script type="text/javascript" src="http://' + urldomain + '/jssdk/sha1.js"></script>')
		document.write('<script type="text/javascript" src="http://' + urldomain +
			'/jssdk/wechatjs/brower.js"></script>');
	}

}

/*
 * 网关验签接口
 * appid应用唯一标识
 * interfaceid接口唯一标识
 * interfacecontent接口参数,请使用json格式, 例如:{"siteid":"2","cateid":"1"}
 * fromport端口来源"0"：PC；"1"：APP；"2"：支付宝；"3"：微信
 * signurl签名接口地址
 * gatewayurl网关接口地址
 */
function vaildInterface(appid, interfaceid, interfacecontent, fromport, signurl, gatewayurl) {
	return new Promise(function(resolve, reject) {
		var datestr = (new Date()).valueOf()
		var param = {
			"app_id": appid,
			"interface_id": interfaceid,
			"version": "1.0",
			"biz_content": interfacecontent,
			"charset": "utf-8",
			"timestamp": datestr,
			"origin": fromport,
			"sign": 'signResult'
		}
		let fdDqtq = new FormData()
		fdDqtq.append('app_id', appid)
		fdDqtq.append('interface_id', interfaceid)
		fdDqtq.append('version', '1.0')
		fdDqtq.append('biz_content', interfacecontent)
		fdDqtq.append('charset', 'utf-8')
		fdDqtq.append('timestamp', datestr)
		fdDqtq.append('origin', fromport)
		fdDqtq.append('sign', 'signResult')

		uni.request({
			url: "https://" + urldomaingateway,
			type: 'post',
			dataType: 'json',
			data: fdDqtq,
			contentType: false, // 注意这里应设为false
			processData: false,
			cache: false,
			success: function(data) {
				if (data == null || data == undefined || data == '') {
					reject(false)
				} else {
					var gateWayResult = data.data
					resolve(gateWayResult)
				}
			},
			error: function(data) {
				reject(false)
			}
		})
	})
}

import {
	tools
} from '../common/sm2.js'

//扩展带请求头方法,headers必填参数
function vaildInterfaceWithHeaders(interfaceid, interfacecontent, publicKey, appid, fromport, header, signurl,
	gatewayurl) {
	if (uni.getStorageSync('username')) {
		//! 请求参数加入节水代码
		interfacecontent.jsdm = uni.getStorageSync('username')
	}

	var headers = {
		'Content-Type': 'application/x-www-form-urlencoded'
	}

	// 如果从本地存储得到有token,请求的时候加上token
	if (uni.getStorageSync('token')) {
		//! 请求参数加入token值
		headers.Authorization = 'Bearer ' + uni.getStorageSync('token') //.token.access_token;
	} else {
		if (interfaceid != 'swfwzxdl' && interfaceid != 'swfwzxdlc') {
			uni.showToast({
				title: '当前用户没有相关节水信息!', 
				icon:'none',
				duration: 6000
			})
			return
		}
	}

	// 显示加载中 效果
	uni.showLoading({
		title: "加载中...",
	});
	console.log('params---------' + interfaceid + JSON.stringify(interfacecontent))
	return new Promise(function(resolve, reject) {
		var datestr = (new Date()).valueOf()
		var param = {
			"app_id": 'qyyscxqnsso',
			"interface_id": interfaceid,
			"version": "1.0",
			"biz_content": '04' + tools.isSm2JM(JSON.stringify(interfacecontent), publicKey),
			"charset": "utf-8",
			"timestamp": datestr,
			"origin": fromport,
			"sign": 'signResult',
			"header": headers
		}

		uni.request({
			url: "https://" + urldomaingateway,
			type: 'post',
			dataType: 'json',
			data: param,
			success: function(data) {
				console.log('requestData---------' + interfaceid + JSON.stringify(data))
				uni.hideLoading()
				if (data == null || data == undefined || data == '') {
					reject(false)
					uni.$emit('z-paging-error-emit');
				} else if (data.data.code == 500) {
					uni.showToast({
						title: '网络异常请稍后刷新重试...',
						icon: 'none'
					})
					uni.$emit('z-paging-error-emit');
				} else {
					var gateWayResult = tools.isSm2Dm(data.data.data,
						'425230668da41e682170945688b53ef70ba9ad8608bf2bf2d65db0a1a121a4c5')
					console.log('request---------' + interfaceid + gateWayResult)
					resolve(JSON.parse(gateWayResult))
				}
			},
			error: function(data) {
				uni.hideLoading()
				reject(false)
				uni.$emit('z-paging-error-emit');
			}
		})
	})
}


async function vaildInterfacefn(appid, interfaceid, interfacecontent, fromport, signurl, gatewayurl) {
	var returnData = await vaildInterface(appid, interfaceid, interfacecontent, fromport, signurl, gatewayurl)
	return returnData
}

async function vaildInterfaceWithHeadersfn(appid, interfaceid, interfacecontent, fromport, headers, signurl,
	gatewayurl) {
	var returnData = await vaildInterfaceWithHeaders(appid, interfaceid, interfacecontent, fromport, headers,
		signurl, gatewayurl)
	return returnData
}

//调用方式验证网关接口
vaildInterfacefn().then(value => {
	var data = value
})

vaildInterfaceWithHeadersfn().then(value => {
	var data = value
})



//导出函数
module.exports = {
	vaildInterfaceWithHeaders,
	vaildInterfaceWithHeadersfn
}
